from time import sleep

import pygame


def main():
    pygame.init()
    # 创建一个窗口
    screen_size = (800, 600)
    # font = pygame.font.SysFont("arial", 16)
    font = pygame.font.Font("STXINGKA.TTF",16)


    screen = pygame.display.set_mode((800, 600), 0, 32)
    # 窗口标题
    pygame.display.set_caption("hello world!")

    # 加载并转换图像
    background_image = "bh.jpg"
    mouse_image = "cursor.png"
    background = pygame.image.load(background_image).convert()
    mouse_cursor = pygame.image.load(mouse_image).convert_alpha()

    event_text = []
    while True:

        event = pygame.event.wait()
        event_text.append(str(event))

        if (event.type == pygame.QUIT):
            exit()

        # 全屏切换
        screen = fullscreen(screen,event)

        # 减小cpu利用率， pygame.event.get()时使用，wait()就无所谓了
        sleep(0.01)

        # 将背景画上去
        screen.blit(background, (0, 0))

        # 打印事件文本
        print_event(event_text, screen, screen_size, font)

        """
         将鼠标画上去
        """
        # 获取鼠标位置
        x, y = pygame.mouse.get_pos()

        # 键盘控制鼠标
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x -= 5
            if event.key == pygame.K_RIGHT:
                x += 5
            if event.key == pygame.K_UP:
                y -= 5;
            if event.key == pygame.K_DOWN:
                y += 5
            pygame.mouse.set_pos(x, y)

        # 计算鼠标左上角
        x -= mouse_cursor.get_width() / 2
        y -= mouse_cursor.get_height() / 2
        screen.blit(mouse_cursor, (x, y))

        pygame.display.update()



def fullscreen(screen, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_f:
            screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN, 32)
        elif event.key == pygame.K_q:
            screen = pygame.display.set_mode((800, 600), 0, 32)
    return screen


def print_event(event_text, screen, screen_size, font):
    """
    打印事件
    :param event_text:  事件文本
    :param screen:  窗口屏幕对象
    :param screen_size:  窗口大小
    :param font:  事件字体
    :return: void
    """
    # 切片，减小event_text大小只有一屏幕的数据
    event_text = event_text[-screen_size[1] // font.get_linesize():]

    y = screen_size[1] - font.get_linesize()
    for text in reversed(event_text):
        screen.blit(font.render(text, True, (1, 2, 255)), (0, y))
        y -= font.get_linesize()


main()
